/* Author: Giorgos Mathioudakis */

$(document).ready( function(){


    //Initialize datepicker
    $('.arrdate').datepicker({
        dateFormat: 'dd/mm/yy',
        minDate: 0,
        onSelect: function (){
            set_dates('arr',this);
        }
    });
    $('.depdate').datepicker({
        dateFormat: 'dd/mm/yy',
        minDate: 0,
        onSelect: function (){
            set_dates('dep',this);
        }
    });

    function set_dates(what, input){

        if ($('.arrdate').val()=='' || $('.depdate').val() == ''){
            var toDay = new Date();
            arr_str = toDay.getDate() + "/" + (toDay.getMonth() + 1) + "/" + toDay.getFullYear(),
            dep_str = toDay.getDate()+1 + "/" + (toDay.getMonth() + 1) + "/" + toDay.getFullYear();
            $('.arrdate').val(arr_str);
            $('.depdate').val(dep_str);
        }

        if(what=="arr")
        {
            arrDate = $(input);
            depDate = $(input).siblings('.depdate');
        }
        else
        {
            depDate = $(input);
            arrDate = $(input).siblings('.arrdate');
        }

        if(arrDate.val()!=null)
        {
            arr_str = arrDate.val();
            dep_str = depDate.val();
        }
        else if(toDay != null)
        {
            arr_str = toDay.getDate() + "/" + (toDay.getMonth() + 1) + "/" + toDay.getFullYear();
            dep_str = toDay.getDate()+1 + "/" + (toDay.getMonth() + 1) + "/" + toDay.getFullYear();
        }
        else
        {
            arr_str = "20/6/2012";
            dep_str = "20/6/2012";
        }

        var month_part = 1, date_part = 0,
        arr_parts = arr_str.split('/'), arrive = new Date(),
        dep_parts = dep_str.split('/'), depart = new Date(),
        arr_y = arr_parts[2], arr_m = arr_parts[month_part]-1, arr_d = arr_parts[date_part],
        dep_y = dep_parts[2], dep_m = dep_parts[month_part]-1, dep_d = dep_parts[date_part];

        arrive.setFullYear(arr_y, arr_m, arr_d);
        depart.setFullYear(dep_y, dep_m, dep_d);

        //Get the difference between arrival and departure
        var new_dif = depart.getTime() - arrive.getTime();
        if (new_dif<=0 && what == 'arr'){ //If arrive is later than depart, reset depart
            depart.setTime(arrive.getTime()+dif);
        }else if (new_dif<=0 && what == 'dep'){ //If arrive is later than depart, reset depart
            arrive.setTime(depart.getTime()-dif);
        }
        if (new_dif >0){
            dif = new_dif;
        }

        //Prepare arrival
        var arr_real_month = parseInt(arrive.getMonth())+1,
        dt = arrive.getDate() < 10 ? '' + arrive.getDate() : arrive.getDate(),
        dm = arr_real_month < 10 ? '' + arr_real_month : arr_real_month;
        arr_str = dt + '/' + dm + '/' + arrive.getFullYear();

        //Prepare departure
        var dep_real_month = parseInt(depart.getMonth())+1,
        ddt = depart.getDate() < 10 ? '' + depart.getDate() : depart.getDate(),
        ddm = dep_real_month < 10 ? '' + dep_real_month : dep_real_month;
        dep_str = ddt + '/' + ddm + '/' + depart.getFullYear();

        arrDate.val(arr_str);
        depDate.val(dep_str);
    }
    set_dates();

});
